package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.StadiumListAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.StadiumDesListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.widge.TimerSelector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 场馆 -- 场地列表
 */
public class StadiumListActivity extends BaseAppCompatActivity implements XRecyclerView.LoadingListener, OnItemClickListener, TimerSelector.OkClickListener {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.xrcv_list)
    XRecyclerView mXrcv_list;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_time)
    TextView tvTime;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;


    private List<StadiumDesListBean.DataBean.ListBean> mDatas;
    private StadiumListAdapter mAdapter;

    private String id;
    private String title;


    private TimerSelector ts_date;
    private String date; // 当前选择的日期

    private TimerSelector ts_time;
    private String time;

    @Override
    protected int contentView() {
        return R.layout.activity_stadium_lsit;
    }

    @Override
    protected void initViews() {
        mDatas = new ArrayList<>();

        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_list.setLayoutManager(manager);
        mXrcv_list.setPullRefreshEnabled(true);
        mXrcv_list.setLoadingMoreEnabled(true);
        mAdapter = new StadiumListAdapter(this, mDatas, R.layout.empty_view, R.layout.item_stadium_des_list);

        ts_date = new TimerSelector(this, "yyyy-MM-dd");
        ts_date.setOkClickListener(this);
        ts_time = new TimerSelector(this, "HH:mm");
        ts_time.setOkClickListener(this);
    }

    @Override
    protected void initEvents() {

        mXrcv_list.setAdapter(mAdapter);
        mXrcv_list.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null) {
            if (intent.getExtras() != null) {
                id = intent.getExtras().getString(Constants.KEY.STADIUM.ITEM_ID);
                title = intent.getExtras().getString(Constants.KEY.STADIUM.ITEM_TITLE);
                mTv_top_title.setText(title);
            }
        }
        if (id != null) {
            getList();
        } else {
            showMsg("场馆ID不存在");
        }
    }

    private void getList() {

        showProgressDialog();
        HttpUitls.onGet("unit_child", RequestFactory.getInstance().unit_child(page + "", limit + "", id, date, time), new OnResponeListener<StadiumDesListBean>() {
            @Override
            public void onSuccess(StadiumDesListBean StadiumDesListBean) {
                Logger.d("unit_child==>>", JsonUtil.getJsonFromObj(StadiumDesListBean));
                if (StadiumDesListBean.code == 0) {
                    totalPage = StadiumDesListBean.totalPage;
                    if (StadiumDesListBean.data.list != null && StadiumDesListBean.data.list.size() > 0) {
                        mXrcv_list.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = StadiumDesListBean.data.list;
                        } else {
                            mDatas.addAll(StadiumDesListBean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        StadiumDesListBean.DataBean.ListBean empty = new StadiumDesListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_list.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(StadiumDesListBean.msg);
                }
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("unit_child==>>", error);
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
            }
        });
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page >= totalPage) {
            mXrcv_list.setNoMore(true, true, "没有更多数据了");
        } else {
            page++;
            getList();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.STADIUM.ITEM_ID, mDatas.get(position).unitId);
        bundle.putString(Constants.KEY.STADIUM.ITEM_TITLE, mDatas.get(position).unitName);
        bundle.putDouble(Constants.KEY.STADIUM.ITEM_FEE, mDatas.get(position).rentFee);
        bundle.putInt(Constants.KEY.STADIUM.ITEM_UNIT_TYPE, mDatas.get(position).unitType);
        bundle.putInt(Constants.KEY.STADIUM.ITEM_BOOK_STATUS, mDatas.get(position).bookStatus);
        bundle.putString(Constants.KEY.STADIUM.ITEM_DES_HREF, Constants.URL.IP + "/unit/detail/" + mDatas.get(position).unitId);

        startActivity(StadiumListDesActivity.class, false, bundle, 0);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            finish();
        }
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    @OnClick({R.id.tv_date, R.id.tv_time,R.id.fl_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_date:
                ts_date.show(tvDate, 0);
                break;
            case R.id.tv_time:
                ts_time.show(tvTime, 1);
                break;
            case R.id.fl_back:
                finish();
                break;
        }
    }

    @Override
    public void okClick(View view, String date, long time, int flag) {
        if (flag == 0) {
            tvDate.setText(date);
            this.date = date;
        }
        if (flag == 1){
            tvTime.setText(date);
            this.time = date;
        }
        if (TextUtils.isEmpty(this.date) || TextUtils.isEmpty(this.time)){
            return;
        }
        getList();
    }
}
